﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;

namespace Zyg.SSO.Web.User
{
    public partial class Modify : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            Zyg.SSO.Core.Business.Account account = (Zyg.SSO.Core.Business.Account)Session["User"];

            if (account == null)
            {
                Response.Redirect("~/Login.aspx");
            }

            AccountEmail.Text = account.Email;
            AccountName.Text = account.Name;
            AccountRemark.Text = account.Remark;
            AccountTel.Text = account.Telephone;

            if (!string.IsNullOrEmpty(account.PictureAddress))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath(account.PictureAddress));

                if (fileInfo.Exists)
                {
                    AccountPic.ImageUrl = account.PictureAddress;
                }
                else
                {
                    AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
                }
            }
            else
            {
                AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            Zyg.SSO.Core.Business.Account account = (Zyg.SSO.Core.Business.Account)Session["User"];

            if (account == null)
            {
                Response.Redirect("~/Login.aspx");
            }

            account.Name = AccountName.Text;
            account.Telephone = AccountTel.Text;
            account.Remark = AccountRemark.Text;


            if (ModifyPicHidden.Value.Equals("true") && FileAccountPicutre.HasFile)
            {
                string accountContentPath = Server.MapPath("~/Content/Account/");
                string extension = Zyg.Utility.Helper.FileHelper.GetFileExtension(FileAccountPicutre.FileName);

                string fileName = account.Id.ToString() + extension;

                account.PictureAddress = "~/Content/Account/" + fileName;

                FileAccountPicutre.SaveAs(accountContentPath + fileName);
            }

            account.Save();

            Session["User"] = account;

            Response.Redirect("Mine.aspx");
        }
    }
}
